//
//  FyUILib.m
//  FyUILib
//
//  Created by luoc on 16/2/6.
//  Copyright © 2016年 luoc. All rights reserved.
//

#import "FyUILib.h"
#import "UIControl.h"
#import "UIManager.h"
#import "UIDlgBuild.h"
#define LISTELEMENT_REMOVESUBVIEW   0
@interface CCommonFunUI()
{
    UIView*     _parentView;
    NSString    *_name;
    NSString    *_textInfo;
    NSString    *_textInfo2;
    NSString    *_textInfo3;
    
    BOOL        _replaceTextOff;
}
@end
@implementation CCommonFunUI
@synthesize IsWidthSet, IsHeightSet, IsFloat;
@synthesize fLeft, fRight, fTop, fBottom;
@synthesize pManagerUI, _delegateView;
@synthesize szMargin;
- (void)dealloc
{

}

- (id)initWithDelegateView:(UIView*)view :(UIManager*)pManager
{
    self = [super init];
    if (self) {
        IsWidthSet = NO;
        IsHeightSet = NO;
        IsFloat = NO;
        _textInfo = nil;
        _textInfo2 = nil;
        _textInfo3 = nil;
        _delegateView = view;
        _replaceTextOff = YES;
        _parentView = view.superview;
        pManagerUI = pManager;
        szMargin.origin.x = szMargin.origin.y = szMargin.size.width = szMargin.size.height = 0;
    }
    return self;
}

- (void)SetManager:(UIManager*)pManager
{
    pManagerUI = pManager;
}

- (NSString*)name
{
    if (_name == nil) {
        return @"";
    }
    return _name;
}

- (void)ChangeTextInfo
{
    if (_delegateView && _textInfo && _replaceTextOff) {
        id<UIAttribute> pPoint = (id<UIAttribute>)_delegateView;
        [pPoint SetAttribute:"text" :[_textInfo UTF8String]];
    }
}

- (NSString*)getLanString:(const char*)value
{
    if (_textInfo == nil && !value) {
        return @"";
    }
    if (!_replaceTextOff) {
        return [NSString stringWithUTF8String:value];
    }
    if (_textInfo == nil) {
        _textInfo = [NSString stringWithUTF8String:value];
    }
    return [UIManager getTextOfLanguageTag:_textInfo];
}

- (NSString*)getLanString2:(const char*)value
{
    if (_textInfo2 == nil && !value) {
        return @"";
    }
    if (_textInfo2 == nil) {
        _textInfo2 = [NSString stringWithUTF8String:value];
    }
    if (!_replaceTextOff) {
        return _textInfo2;
    }
    return [UIManager getTextOfLanguageTag:_textInfo2];
}

- (NSString*)getLanString3:(const char*)value
{
    if (_textInfo3 == nil && !value) {
        return @"";
    }
    if (_textInfo3 == nil) {
        _textInfo3 = [NSString stringWithUTF8String:value];
    }
    if (!_replaceTextOff) {
        return _textInfo3;
    }
    return [UIManager getTextOfLanguageTag:_textInfo3];
}

- (NSString*)getTextTag:(int)tag
{
    if (tag == 1 && _textInfo) {
        return _textInfo;
    }else if (tag == 2 && _textInfo2) {
        return _textInfo2;
    }else if (tag == 3 && _textInfo3) {
        return _textInfo3;
    }
    return @"";
}

+ (void)SetFrameOfVertical:(CGRect)frame :(UIView*)parentView
{
    NSUInteger count = [parentView.subviews count];
    if (count == 0) {
        return;
    }
    
    CGFloat w = 0; int i = 0;
    for (UIView* view in parentView.subviews) {
#if !LISTELEMENT_REMOVESUBVIEW
        if (![[NSStringFromClass([view class]) substringToIndex:1] isEqualToString:@"C"]) {
            i++;
            continue;
        }
#endif
        id<UIAttribute> v = (id<UIAttribute>)view;
        if ([v commonFun].IsFloat || [view isHidden]) {
            i++;
            continue;
        }
        if ([[v commonFun] IsWidthSet]) {
            i++;
            w += view.frame.size.width;
        }
    }
    
    if (frame.size.width - w <= 0) {
        int originX = 0;
        for (UIView* view in parentView.subviews) {
#if !LISTELEMENT_REMOVESUBVIEW
            if (![[NSStringFromClass([view class]) substringToIndex:1] isEqualToString:@"C"]) {
                continue;
            }
#endif
            id<UIAttribute> v = (id<UIAttribute>)view;
            if ([v commonFun].IsFloat) continue;
            if ([view isHidden]) {
                continue;
            }
            CGFloat x = [v commonFun].szMargin.origin.x;
            CGFloat y = [v commonFun].szMargin.origin.y;
            CGFloat x2 = [v commonFun].szMargin.size.width;
            CGFloat y2 = [v commonFun].szMargin.size.height;
            if ([[v commonFun] IsWidthSet]) {
                if ([[v commonFun] IsHeightSet]) {
                    [view setFrame:CGRectMake(originX + x, y, view.frame.size.width - x2, view.frame.size.height - y2)];
                }else{
                    [view setFrame:CGRectMake(originX + x, y, view.frame.size.width - x2, frame.size.height - y2)];
                }
                originX += view.frame.size.width;
            }else{
                [view setFrame:CGRectMake(originX + x, y, 0, view.frame.size.height)];
            }
        }
    }else{
        w = (frame.size.width - w) / (count-i);
        int originX = 0;
        for (UIView* view in parentView.subviews) {
#if !LISTELEMENT_REMOVESUBVIEW
            if (![[NSStringFromClass([view class]) substringToIndex:1] isEqualToString:@"C"]) {
                continue;
            }
#endif
            id<UIAttribute> v = (id<UIAttribute>)view;
            if ([v commonFun].IsFloat) continue;
            if ([view isHidden]) {
                continue;
            }
            
            CGFloat x = [v commonFun].szMargin.origin.x;
            CGFloat y = [v commonFun].szMargin.origin.y;
            CGFloat x2 = [v commonFun].szMargin.size.width;
            CGFloat y2 = [v commonFun].szMargin.size.height;
            if ([[v commonFun] IsWidthSet]) {
                if ([[v commonFun] IsHeightSet]) {
                    [view setFrame:CGRectMake(originX + x, y, view.frame.size.width - x2, view.frame.size.height - y2)];
                }else{
                    [view setFrame:CGRectMake(originX + x, y, view.frame.size.width - x2, frame.size.height - y2)];
                }
                
                originX += view.frame.size.width;
            }else{
                if ([[v commonFun] IsHeightSet]) {
                    [view setFrame:CGRectMake(originX + x, y, w - x2, view.frame.size.height - y2)];
                }else{
                    [view setFrame:CGRectMake(originX + x, y, w - x2, frame.size.height -y2)];
                }
                
                originX += w;
            }
        }
    }
}

+ (void)SetFrameOfHorizontal:(CGRect)frame :(UIView*)parentView
{
    NSUInteger count = [parentView.subviews count];
    if (count == 0) {
        return;
    }
    CGFloat h = 0; int i=0;
    for (UIView* view in parentView.subviews) {
#if !LISTELEMENT_REMOVESUBVIEW
        if (![[NSStringFromClass([view class]) substringToIndex:1] isEqualToString:@"C"]) {
            i++;
            continue;
        }
#endif
        id<UIAttribute> v = (id<UIAttribute>)view;
        if ([v commonFun].IsFloat || [view isHidden]) {
            i++;
            continue;
        }
        if ([[v commonFun] IsHeightSet]) {
            h += view.frame.size.height;
            i ++;
        }
    }
    
    if (frame.size.height - h <= 0) {
        int originY = 0;
        for (UIView* view in parentView.subviews) {
#if !LISTELEMENT_REMOVESUBVIEW
            if (![[NSStringFromClass([view class]) substringToIndex:1] isEqualToString:@"C"]) {
                continue;
            }
#endif
            id<UIAttribute> v = (id<UIAttribute>)view;
            if ([v commonFun].IsFloat) continue;
            if ([view isHidden]) continue;
            
            CGFloat x = [v commonFun].szMargin.origin.x;
            CGFloat y = [v commonFun].szMargin.origin.y;
            CGFloat x2 = [v commonFun].szMargin.size.width;
            CGFloat y2 = [v commonFun].szMargin.size.height;
            if ([[v commonFun] IsHeightSet]) {
                if ([[v commonFun] IsWidthSet]) {
                    [view setFrame:CGRectMake(x, originY + y, view.frame.size.width - x2, view.frame.size.height - y2)];
                }else{
                    [view setFrame:CGRectMake(x, originY + y, frame.size.width - x2, view.frame.size.height -y2)];
                }
                originY += view.frame.size.height;
            }else{
                [view setFrame:CGRectMake(x, originY + y, view.frame.size.width, 0)];
            }
        }
    }else{
        h = (frame.size.height - h) / (count-i);
        int originY = 0;
        for (UIView* view in parentView.subviews) {
#if !LISTELEMENT_REMOVESUBVIEW
            if (![[NSStringFromClass([view class]) substringToIndex:1] isEqualToString:@"C"]) {
                continue;
            }
#endif
            id<UIAttribute> v = (id<UIAttribute>)view;
            if ([v commonFun].IsFloat) continue;
            if ([view isHidden]) continue;
            
            CGFloat x = [v commonFun].szMargin.origin.x;
            CGFloat y = [v commonFun].szMargin.origin.y;
            CGFloat x2 = [v commonFun].szMargin.size.width;
            CGFloat y2 = [v commonFun].szMargin.size.height;
            if ([[v commonFun] IsHeightSet]) {
                if ([[v commonFun] IsWidthSet]) {
                    [view setFrame:CGRectMake(x, originY + y, view.frame.size.width - x2, view.frame.size.height - y2)];
                }else{
                    [view setFrame:CGRectMake(x, originY + y, frame.size.width - x2, view.frame.size.height)];
                }
                
                originY += view.frame.size.height;
            }else{
                if ([[v commonFun] IsWidthSet]) {
                    [view setFrame:CGRectMake(x, originY + y, view.frame.size.width - x2, h - y2)];
                }else{
                    [view setFrame:CGRectMake(x, originY + y, frame.size.width - x2, h - y2)];
                }
                
                originY += h;
            }
        }
    }
}

+ (void)SetFrameOfOverlap:(CGRect)frame :(UIView*)parentView{
    NSUInteger count = [parentView.subviews count];
    if (count == 0) {
        return;
    }
    for (UIView* view in parentView.subviews) {
#if !LISTELEMENT_REMOVESUBVIEW
        if (![[NSStringFromClass([view class]) substringToIndex:1] isEqualToString:@"C"]) {
            continue;
        }
#endif
        [view setFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
    }
}

+ (UIColor*)getColorValue:(const char*)value{
    int r=0,g=0,b=0,a=0;
    if(4 == sscanf(value, "#%02x%02x%02x%02x", &a, &r, &g, &b)){
        return [UIColor colorWithRed:(float)r/255 green:(float)g/255 blue:(float)b/255 alpha:(float)a/255];
    }
    return [UIColor redColor];
}

+ (CGSize)getSizeValue:(const char*)value{
    CGSize size = {0};
    float w=0, h=0;
    if (2 == sscanf(value, "%f, %f", &w, &h)) {
        size.width = w;
        size.height = h;
        return size;
    }
    return size;
}

+ (CGRect)getRectValue:(const char*)value
{
    CGRect rt = {0};
    float x=0, y=0, w=0, h=0;
    if (4 == sscanf(value, "%f,%f,%f,%f", &x, &y, &w, &h)) {
        rt.size.width = w;
        rt.size.height = h;
        rt.origin.x = x;
        rt.origin.y = y;
        return rt;
    }
    return rt;
}

- (UIView*)parentView
{
    return _parentView;
}

- (void)setBackgroundColor:(const char*)value
{
    [_delegateView setBackgroundColor:[CCommonFunUI getColorValue:value]];
}

- (void)setBorder:(const char*)value
{
    int r=0,g=0,b=0,a=0, border=0;
    char style[20] = {0};
    if(6 == sscanf(value, "%dpx %20s #%02x%02x%02x%02x", &border, style, &a, &r, &g, &b)){
        _delegateView.layer.borderColor = [UIColor colorWithRed:(float)r/255 green:(float)g/255 blue:(float)b/255 alpha:(float)a/255].CGColor;
        _delegateView.layer.borderWidth = border;
    }
}

- (void)handleTap:(UITapGestureRecognizer *)recognizer {
    [pManagerUI OnNotify:((id<UIAttribute>)_delegateView) event:FYEVENT_TAP param:NULL];
}

- (void)setAttribute:(const char*)pstrName value:(const char*)value
{
    if (strcmp(pstrName, "bkcolor") == 0) {
        [self setBackgroundColor:value];
    }else if (strcmp(pstrName, "width") == 0){
        [self setWidthSet:YES];
        CGRect rct = _delegateView.frame;
        [_delegateView setFrame:CGRectMake(rct.origin.x, rct.origin.y, atoi(value), rct.size.height)];
    }else if (strcmp(pstrName, "height") == 0){
        [self setHeightSet:YES];
        CGRect rct = _delegateView.frame;
        [_delegateView setFrame:CGRectMake(rct.origin.x, rct.origin.y, rct.size.width, atoi(value))];
    }else if(strcmp(pstrName, "removeheight") == 0){
        [self setHeightSet:NO];
    }else if(strcmp(pstrName, "removewidth") == 0){
        [self setWidthSet:NO];
    }else if (strcmp(pstrName, "name") == 0){
        _name = [NSString stringWithUTF8String:value];
        [pManagerUI AddObject:value :_delegateView];
    }else if (strcmp(pstrName, "border") == 0){
        [self setBorder:value];
    }else if (strcmp(pstrName, "bordersize") == 0){
        _delegateView.layer.borderWidth = atof(value);
    }else if (strcmp(pstrName, "bordercolor") == 0){
        _delegateView.layer.borderColor = [CCommonFunUI getColorValue:value].CGColor;
    }else if (strcmp(pstrName, "corner") == 0){
        _delegateView.layer.cornerRadius = atof(value);
        _delegateView.layer.masksToBounds = YES;
    }else if (strcmp(pstrName, "hidden") == 0){
        if (strcmp(value, "true") == 0) {
            [_delegateView setHidden:YES];
        }else{
            [_delegateView setHidden:NO];
        }
    }else if (strcmp(pstrName, "float") == 0){
        if (strcmp(value, "true") == 0) {
            IsFloat = YES;
            [pManagerUI AddFloatObject:_delegateView];
        }else{
            IsFloat = NO;
            [pManagerUI removeFloatObject:_delegateView];
        }
    }else if (strcmp(pstrName, "left") == 0){
        self.fLeft = atof(value);
    }else if (strcmp(pstrName, "right") == 0){
        self.fRight = atof(value);
    }else if (strcmp(pstrName, "top") == 0){
        self.fTop = atof(value);
    }else if (strcmp(pstrName, "bottom") == 0){
        self.fBottom = atof(value);
    }else if (strcmp(pstrName, "margin") == 0){
        szMargin = [CCommonFunUI getRectValue:value];
    }else if(strcmp(pstrName, "tapEvent") == 0) {
        UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
        recognizer.numberOfTapsRequired = atoi(value);
        recognizer.numberOfTouchesRequired = 1;
        [_delegateView addGestureRecognizer:recognizer];
    }else if(strcmp(pstrName, "tag") == 0){
        _delegateView.tag = atoi(value);
    }else if(strcmp(pstrName, "lgOff") == 0){
        if (strcmp(value, "true") == 0) {
            _replaceTextOff = NO;
        }else{
            _replaceTextOff = YES;
        }
    }
}

- (void)setHeightSet:(BOOL)bHeightSet
{
    IsHeightSet = bHeightSet;
}

- (void)setWidthSet:(BOOL)bWidthSet
{
    IsWidthSet = bWidthSet;
}

- (void)setFloat:(BOOL)bFloat
{
    IsFloat = bFloat;
}

- (void)setLeft:(CGFloat)f
{
    fLeft = f;
    fRight = -1.0;
}

- (void)setRight:(CGFloat)f
{
    fRight = f;
    fLeft = -1.0f;
}

- (void)setTop:(CGFloat)f
{
    fTop = f;
    fBottom = -1.0f;
}

- (void)setBottom:(CGFloat)f
{
    fBottom = f;
    fTop = -1.0f;
}

- (void)setMargin:(CGRect)size
{
    szMargin = size;
}

@end



/********************************************************/
/*                                                      */
/********************************************************/
@interface CEditUI()
{
    CCommonFunUI    *commonFun;
    NSInteger       iLayoutStyle;
    CGFloat         fTextIndent;
}
@end
@implementation CEditUI
- (id)initWithUIManager:(UIManager*)pManager{
    self = [super init];
    if (self) {
        commonFun = [[CCommonFunUI alloc] initWithDelegateView:self :pManager];
        self.delegate = self;
        self.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
        self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        fTextIndent = 0;
    }
    return self;
}

- (CCommonFunUI*)commonFun
{
    return commonFun;
}

- (void)SetAttribute:(const char*)pstrName :(const char*)value
{
    if (strcmp(pstrName, "text") == 0) {
        [self setText:[NSString stringWithUTF8String:value]];
    }else if (strcmp(pstrName, "place") == 0){
        [self setPlaceholder:[commonFun getLanString:value]];
    }else if (strcmp(pstrName, "password") == 0){
        if (strcmp(value, "true") == 0) {
            self.secureTextEntry = YES;
        }else{
            self.secureTextEntry = NO;
        }
    }else if (strcmp(pstrName, "align") == 0){
        if (strcmp(value, "center") == 0) {
            self.textAlignment = NSTextAlignmentCenter;
        }else if (strcmp(value, "right") == 0){
            self.textAlignment = NSTextAlignmentRight;
        }else{
            self.textAlignment = NSTextAlignmentLeft;
        }
    }else if (strcmp(pstrName, "textcolor") == 0){
        self.textColor = [CCommonFunUI getColorValue:value];
    }else if (strcmp(pstrName, "font") == 0){
        self.font = [UIFont systemFontOfSize:atof(value)];
    }else if (strcmp(pstrName, "style") == 0) {
        iLayoutStyle = atoi(value);
    }else if(strcmp(pstrName, "textIndent") == 0){
        fTextIndent = atof(value);
    }else if(strcmp(pstrName, "leftview") == 0){
        int w = 0,h = 0;
        char style[20] = {0}, imageNormal[40] ={0}, imagePress[40], name[40];
        if(6 == sscanf(value, "%20s %40s {%d,%d} %40s %40s", style, name, &w, &h, imageNormal, imagePress)){
            self.leftView = nil;
            if (strcasecmp(style, "button") == 0) {
                CButtonUI* pButton = [[CButtonUI alloc] initWithUIManager:[commonFun pManagerUI]];
                [pButton setFrame:CGRectMake(0, 0, w, h)];
                [pButton SetAttribute:"normalImage" :imageNormal];
                [pButton SetAttribute:"selectImage" :imagePress];
                [pButton SetAttribute:"name" :name];
                [pButton SetAttribute:"downEvent" :"true"];
                self.leftView = pButton;
                self.leftViewMode = UITextFieldViewModeAlways;
            }
        }
    }else if(strcmp(pstrName, "rightview") == 0){
        int w = 0,h = 0;
        char style[20] = {0}, imageNormal[40] ={0}, imagePress[40], name[40];
        if(6 == sscanf(value, "%20s %40s {%d,%d} %40s %40s", style, name, &w, &h, imageNormal, imagePress)){
            self.rightView = nil;
            if (strcasecmp(style, "button") == 0) {
                CButtonUI* pButton = [[CButtonUI alloc] initWithUIManager:[commonFun pManagerUI]];
                [pButton setFrame:CGRectMake(0, 0, w, h)];
                [pButton SetAttribute:"normalImage" :imageNormal];
                [pButton SetAttribute:"selectImage" :imagePress];
                [pButton SetAttribute:"name" :name];
                [pButton SetAttribute:"downEvent" :"true"];
                self.rightView = pButton;
                self.rightViewMode = UITextFieldViewModeAlways;
            }
        }
    }else{
        [commonFun setAttribute:pstrName value:value];
        if (strcmp(pstrName, "corner") == 0) {
            self.borderStyle = UITextBorderStyleRoundedRect;
        }
    }
}

- (BOOL)textFieldShouldReturn:(UITextField*)textField{
    [textField resignFirstResponder];
    [commonFun.pManagerUI OnNotify:self event:FYEVENT_TX_RETURN param:NULL];
    return YES;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    [commonFun.pManagerUI OnNotify:self event:FYEVENT_TX_BEGIN param:NULL];
}

- (CGRect)textRectForBounds:(CGRect)bounds
{
    bounds = [super textRectForBounds:bounds];
    CGRect inset = CGRectMake(bounds.origin.x + fTextIndent, bounds.origin.y, bounds.size.width - fTextIndent, bounds.size.height);
    return inset;
}

- (CGRect)editingRectForBounds:(CGRect)bounds
{
    bounds = [super editingRectForBounds:bounds];
    CGRect inset = CGRectMake(bounds.origin.x + fTextIndent, bounds.origin.y, bounds.size.width - fTextIndent, bounds.size.height);
    return inset;
}

- (void)setNeedsDisplay
{
    [super setNeedsDisplay];
    [self setFrame:self.frame];
}

- (void)setFrame:(CGRect)frame
{
    [super setFrame:frame];
    if (iLayoutStyle == 0) {
        [CCommonFunUI SetFrameOfVertical:frame :self];
    }else if(iLayoutStyle == 1){
        [CCommonFunUI SetFrameOfHorizontal:frame :self];
    }else if (iLayoutStyle == 3){
        [CCommonFunUI SetFrameOfOverlap:frame :self];
    }
}
@end



/********************************************************/
/*                                                      */
/********************************************************/
@interface CGifUI()
{
    CCommonFunUI    *commonFun;
}
@end

@implementation CGifUI
- (id)initWithUIManager:(UIManager*)pManager{
    //NSURL *fileUrl = [[NSBundle mainBundle] URLForResource:[NSString stringWithUTF8String:"wait"] withExtension:@"gif"];
    //self = [super initWithCenter:CGPointZero fileURL:fileUrl];
    self = [super initWithCenter:CGPointZero fileURL:nil];
    if (self) {
        commonFun = [[CCommonFunUI alloc] initWithDelegateView:self :pManager];
    }
    return self;
}

- (void)dealloc
{

}

- (CCommonFunUI*)commonFun
{
    return commonFun;
}

- (void)setFrame:(CGRect)frame
{
    [super setFrame:frame];
    
    CGFloat width = [super getWidth];
    CGFloat height = [super getHeight];
    
    if (width > 0 && height > 0 && frame.size.width > 0 && frame.size.height > 0) {
        CGFloat fw = width / frame.size.width;
        CGFloat fh = height / frame.size.height;
        CGFloat x =0, y = 0;
        if (fw > fh) {
            width = frame.size.width;
            height = height * (1 / fw);
            y = (frame.size.height - height) / 2;
        }else{
            height = frame.size.height;
            width = width * (1/fh);
            x = (frame.size.width - width) / 2;
        }
        [self.layer setFrame:CGRectMake(x + frame.origin.x, y + frame.origin.y, width, height)];
    }
}

- (void)SetAttribute:(const char*)pstrName :(const char*)value
{
    if (strcmp(pstrName, "src") == 0) {
        NSURL *fileUrl = [[NSBundle mainBundle] URLForResource:[NSString stringWithUTF8String:value] withExtension:@"gif"];
        [self loadGif:fileUrl];
    }else{
        [commonFun setAttribute:pstrName value:value];
    }
}
@end

/********************************************************/
/*                                                      */
/********************************************************/
@interface CImageUI()
{
    CCommonFunUI    *commonFun;
}
@end

@implementation CImageUI
- (id)initWithUIManager:(UIManager*)pManager{
    self = [super init];
    if (self) {
        commonFun = [[CCommonFunUI alloc] initWithDelegateView:self :pManager];
        self.contentMode = UIViewContentModeCenter;
    }
    return self;
}

- (CCommonFunUI*)commonFun
{
    return commonFun;
}

- (void)SetAttribute:(const char*)pstrName :(const char*)value
{
    if (strcmp(pstrName, "src") == 0) {
        self.image = [UIImage imageNamed:[NSString stringWithUTF8String:value]];
    }else if(strcmp(pstrName, "mode") == 0){
        self.contentMode = atoi(value);
    }else{
        [commonFun setAttribute:pstrName value:value];
    }
}
@end


/********************************************************/
/*                                                      */
/********************************************************/
#pragma mark -- CListUI
@interface CListUI()
{
    CCommonFunUI    *commonFun;
    NSMutableArray  *listSection;
    NSMutableArray  *listData;
    NSMutableArray  *listUIManager;
    CGFloat         itemHeight;
    NSInteger       iCellSelectionStyle;
    NSInteger       iCellAccessoryType;
    NSString        *cellString;
    UIManager       *pHeadManagerUI;
    UIManager       *pFootManagerUI;
    CGFloat         fFootHeight;
    CGFloat         fHeadHeight;
    
    CGSize          *secSize;
    int             secSizeCnt;
}
@end
@implementation CListUI
- (id)initWithUIManager:(UIManager*)pManager{
    self = [super init];
    if (self) {
        commonFun = [[CCommonFunUI alloc] initWithDelegateView:self :pManager];
        listUIManager = [[NSMutableArray alloc] init];
        listSection = [[NSMutableArray alloc] init];
        listData = [[NSMutableArray alloc] init];
        self.delegate = self;
        self.dataSource = self;
        iCellSelectionStyle = iCellAccessoryType = 0;
        fFootHeight = fHeadHeight = 0;
        
        secSizeCnt = 4;
        secSize = (CGSize*)malloc(sizeof(CGSize)*secSizeCnt);
    }
    return self;
}

- (void)dealloc
{
    free(secSize);
}

- (CCommonFunUI*)commonFun
{
    return commonFun;
}

- (CGFloat)getItemHeight{
    return itemHeight;
}

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{   
    [commonFun.pManagerUI OnNotify:self event:FYEVENT_LIST_SCROLLBEGIN param:NULL];
}

- (BOOL)addValue:(NSString*)name :(NSString*)attr :(NSString*)value :(NSInteger)index
{
    NSMutableArray* array = nil;
    if ([listData count] > index) {
        array = [listData objectAtIndex:index];
    }else if ([listData count] == index){
        array = [[NSMutableArray alloc] init];
        [listData addObject:array];
    }else{
        return NO;
    }
    NSArray* arr = [[NSArray alloc] initWithObjects:name, attr, value, nil];
    [array addObject:arr];
    return YES;
}

- (NSString*)GetValue:(NSString*)name :(NSString*)attr :(NSInteger)section :(NSInteger)index
{
    if ([listData count] < section) return nil;
    NSMutableArray* array = [listData objectAtIndex:index];
    NSArray* obj = [array objectAtIndex:index];
    return [obj objectAtIndex:2];
}

- (void)removeValue:(NSIndexPath*)indexPath
{
    [listData removeObjectAtIndex:indexPath.row];
    /*
    if ([listData count]) {
        [[listSection objectAtIndex:indexPath.section] removeObjectAtIndex:indexPath.row];
    }else{
        [listData removeObjectAtIndex:indexPath.row];
    }*/
}

- (void)removeAll {
    [listData removeAllObjects];
}

- (void)AddListElement:(CListElementUI*)element
{
    if ([listSection count] == 0) {
        [self addSection];
    }
    [[listSection objectAtIndex:([listSection count]-1)] addObject:element];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [listSection count];
}

- (void)addSection
{
    NSMutableArray* arr = [[NSMutableArray alloc] init];
    [listSection addObject:arr];
    
    if ([listSection count] > secSizeCnt) {
        secSizeCnt += 4;
        secSize = realloc(secSize, sizeof(CGSize)*secSizeCnt);
        memset(&secSize[secSizeCnt-4], 0x00, sizeof(CGSize)*4);
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return (secSizeCnt>section)?secSize[section].height:0.0f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return (secSizeCnt>section)?secSize[section].width:0.0f;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (cellString == nil) {
        return [[listSection objectAtIndex:section] count];
    }
    return [listData count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (cellString == nil) {
        CListElementUI* pCell = [[listSection objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
        if ([[pCell commonFun] IsHeightSet]) {
            return pCell.frame.size.height;
        }
    }
    return itemHeight;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [commonFun.pManagerUI OnNotify:self event:FYEVENT_TB_SELECTED param:(void*)indexPath];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (cellString == nil) {
        static int iiii =0;
        iiii ++;
        CListElementUI* cell = [[listSection objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
        cell.selectionStyle = cell._iSelectType < 0?iCellSelectionStyle:cell._iSelectType;
        cell.accessoryType = cell._iAccessoryType < 0?iCellAccessoryType:cell._iAccessoryType;
        
        return cell;
    }
    CListElementUI* cell = [tableView dequeueReusableCellWithIdentifier:[NSString stringWithFormat:@"cell_%p", tableView]];
    if (cell == nil) {
        UIManager* pManagerUI = [[UIManager alloc] initWithUIManagerDelegate:[commonFun.pManagerUI mainController]];
        //UIManager* pManagerUI = [[UIManager alloc] init];
        UIDlgBuild* build = [[UIDlgBuild alloc] init];
        
        NSString *file = [[NSBundle mainBundle] pathForResource:cellString ofType:@"xml"];
        cell = (CListElementUI*)[build Create:[file UTF8String] :^UIView *(const char *pstrClass) {
            return nil;
        } :pManagerUI :self];
        
        if (cell == nil)
            return nil;
        cell.selectionStyle = cell._iSelectType < 0?iCellSelectionStyle:cell._iSelectType;
        cell.accessoryType = cell._iAccessoryType < 0?iCellAccessoryType:cell._iAccessoryType;
        cell.iFlag = [listUIManager count];
        [listUIManager addObject:pManagerUI];
    }
    if (cell == nil || [[listSection objectAtIndex:0] count] <= cell.iFlag || [listUIManager count] <= cell.iFlag || [listData count] <= indexPath.row)
        return nil;
    
    UIManager* pManagerUI = [listUIManager objectAtIndex:cell.iFlag];
    NSMutableArray* array = [listData objectAtIndex:indexPath.row];
    for (int i=0; i<[array count]; i++) {
        NSArray* arr = [array objectAtIndex:i];
        id<UIAttribute> obj = (id<UIAttribute>)[pManagerUI GetViewOfName:[[arr objectAtIndex:0] UTF8String]];
        [obj SetAttribute:[[arr objectAtIndex:1] UTF8String] :[[arr objectAtIndex:2] UTF8String]];
    }
    
    return cell;
}

- (void)SetAttribute:(const char*)pstrName :(const char*)value
{
    if (strcmp(pstrName, "itemheight") == 0) {
        itemHeight = atof(value);
    }else if(strcmp(pstrName, "scroll") == 0){
        if (strcmp(value, "true") == 0) {
            self.scrollEnabled = YES;
        }else{
            self.scrollEnabled = NO;
        }
    }else if(strcmp(pstrName, "itemlinestyle") == 0){
        self.separatorStyle = atoi(value) % 3;
    }else if(strcmp(pstrName, "itemselectstyle") == 0){
        iCellSelectionStyle = atoi(value);
    }else if(strcmp(pstrName, "itemxml") == 0){
        cellString = [NSString stringWithUTF8String:value];
    }else if(strcmp(pstrName, "itemaccessorytype") == 0){
        iCellAccessoryType = atoi(value)%4;
    }else if(strcmp(pstrName, "headxml") == 0){
        pHeadManagerUI = [[UIManager alloc] init];
        UIDlgBuild* build = [[UIDlgBuild alloc] init];
        NSString *file = [[NSBundle mainBundle] pathForResource:[NSString stringWithUTF8String:value] ofType:@"xml"];
        self.tableHeaderView = [build Create:[file UTF8String] :^UIView *(const char *pstrClass) {
            return nil;
        } :pHeadManagerUI :self];
    }else if(strcmp(pstrName, "footxml") == 0){
        pFootManagerUI = [[UIManager alloc] init];
        UIDlgBuild* build = [[UIDlgBuild alloc] init];
        NSString *file = [[NSBundle mainBundle] pathForResource:[NSString stringWithUTF8String:value] ofType:@"xml"];
        self.tableFooterView = [build Create:[file UTF8String] :^UIView *(const char *pstrClass) {
            return nil;
        } :pFootManagerUI :self];
    }else if(strcmp(pstrName, "footheight") == 0){
        fFootHeight = atof(value);
    }else if(strcmp(pstrName, "headheight") == 0){
        fHeadHeight = atof(value);
    }else if(strcmp(pstrName, "secheight") == 0){
        CGSize size = [CCommonFunUI getSizeValue:value];
        secSize[[listSection count]-1]= size;
    }else{
        [commonFun setAttribute:pstrName value:value];
    }
}

- (void)setFrame:(CGRect)frame
{
    [super setFrame:frame];
    if (self.tableHeaderView) {
        [self.tableHeaderView setFrame:CGRectMake(0, 0, frame.size.width, fHeadHeight)];
    }
    
    self.contentInset = UIEdgeInsetsMake(fHeadHeight, 0, fFootHeight, 0);
}
@end

#pragma mark -- CListUI
@interface CListUI2()
{
    CCommonFunUI    *commonFun;
    NSMutableArray  *listElement;
    NSMutableArray  *listData;
    NSMutableArray  *listUIManager;
    CGFloat         itemHeight;
    NSInteger       iCellSelectionStyle;
    NSInteger       iCellAccessoryType;
    NSString        *cellString;
    UIManager       *pHeadManagerUI;
    UIManager       *pFootManagerUI;
    CGFloat         fFootHeight;
    CGFloat         fHeadHeight;
    
    NSMutableArray  *ssHeadArray;
}
@end
@implementation CListUI2
- (id)initWithUIManager:(UIManager*)pManager{
    self = [super init];
    if (self) {
        commonFun = [[CCommonFunUI alloc] initWithDelegateView:self :pManager];
        listElement = [[NSMutableArray alloc] init];
        listUIManager = [[NSMutableArray alloc] init];
        listData = [[NSMutableArray alloc] init];
        self.delegate = self;
        self.dataSource = self;
        
        fFootHeight = fHeadHeight = 0;
        
        ssHeadArray = [[NSMutableArray alloc] init];
    }
    return self;
}

- (CCommonFunUI*)commonFun
{
    return commonFun;
}

- (void)setListData:(NSMutableArray*)data
{
    listData = data;
}

- (void)setListssHeadData:(NSMutableArray*)data
{
    ssHeadArray = data;
}

- (BOOL)addValue:(NSString*)name :(NSString*)attr :(NSString*)value :(NSInteger)index
{
    NSMutableArray* array = nil;
    if ([listData count] > index) {
        array = [listData objectAtIndex:index];
    }else if ([listData count] == index){
        array = [[NSMutableArray alloc] init];
        [listData addObject:array];
    }else{
        return NO;
    }
    NSArray* arr = [[NSArray alloc] initWithObjects:name, attr, value, nil];
    [array addObject:arr];
    return YES;
}

- (void)AddListElement:(CListElementUI*)element
{
    [listElement addObject:element];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[listData objectAtIndex:section] count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return itemHeight;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [commonFun.pManagerUI OnNotify:self event:FYEVENT_TB_SELECTED param:(void*)indexPath];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CListElementUI* cell = [tableView dequeueReusableCellWithIdentifier:[NSString stringWithFormat:@"cell_%p", tableView]];
    if (cell == nil) {
        if (cellString) {
            UIManager* pManagerUI = [[UIManager alloc] init];
            UIDlgBuild* build = [[UIDlgBuild alloc] init];
            NSString *file = [[NSBundle mainBundle] pathForResource:cellString ofType:@"xml"];
            cell = (CListElementUI*)[build Create:[file UTF8String] :^UIView *(const char *pstrClass) {
                return nil;
            } :pManagerUI :self];
            
            if (cell == nil)
                return nil;
            cell.selectionStyle = cell._iSelectType < 0?iCellSelectionStyle:cell._iSelectType;
            cell.accessoryType = cell._iAccessoryType < 0?iCellAccessoryType:cell._iAccessoryType;
            cell.iFlag = indexPath.row;
            [listUIManager addObject:pManagerUI];
        }else{
            UITableViewCell* cellT = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[NSString stringWithFormat:@"cell_%p", tableView]];
            cellT.textLabel.text = [[listData objectAtIndex:indexPath.section]objectAtIndex:indexPath.row];
            cellT.selectionStyle = iCellSelectionStyle;
            cellT.accessoryType = iCellAccessoryType;
            return cellT;
        }
    }else if (cellString == nil){
        if ([[listData objectAtIndex:indexPath.section] count] > indexPath.row) {
            cell.textLabel.text = [[listData objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
        }
        
        return cell;
    }
    if (cell == nil || [listElement count] <= cell.iFlag || [listUIManager count] <= cell.iFlag || [listData count] <= indexPath.row)
        return nil;
    
    UIManager* pManagerUI = [listUIManager objectAtIndex:cell.iFlag];
    NSMutableArray* array = [listData objectAtIndex:indexPath.row];
    for (int i=0; i<[array count]; i++) {
        NSArray* arr = [array objectAtIndex:i];
        id<UIAttribute> obj = (id<UIAttribute>)[pManagerUI GetViewOfName:[[arr objectAtIndex:0] UTF8String]];
        [obj SetAttribute:[[arr objectAtIndex:1] UTF8String] :[[arr objectAtIndex:2] UTF8String]];
    }
    
    return cell;
}

#pragma mark -Section的Header的值
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    NSString *key = [ssHeadArray objectAtIndex:section];
    return key;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 20)];
    lab.backgroundColor = [UIColor lightGrayColor];
    lab.text = [ssHeadArray objectAtIndex:section];
    lab.textColor = [UIColor whiteColor];
    return lab;
}

-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return ssHeadArray;
}

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
    return index;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [ssHeadArray count];
}

- (void)SetAttribute:(const char*)pstrName :(const char*)value
{
    if (strcmp(pstrName, "itemheight") == 0) {
        itemHeight = atof(value);
    }else if(strcmp(pstrName, "itemlinestyle") == 0){
        self.separatorStyle = atoi(value) % 3;
    }else if(strcmp(pstrName, "itemselectstyle") == 0){
        iCellSelectionStyle = atoi(value);
    }else if(strcmp(pstrName, "itemxml") == 0){
        cellString = [NSString stringWithUTF8String:value];
    }else if(strcmp(pstrName, "itemaccessorytype") == 0){
        iCellAccessoryType = atoi(value)%4;
    }else if(strcmp(pstrName, "headxml") == 0){
        pHeadManagerUI = [[UIManager alloc] init];
        UIDlgBuild* build = [[UIDlgBuild alloc] init];
        NSString *file = [[NSBundle mainBundle] pathForResource:[NSString stringWithUTF8String:value] ofType:@"xml"];
        self.tableHeaderView = [build Create:[file UTF8String] :^UIView *(const char *pstrClass) {
            return nil;
        } :pHeadManagerUI :self];
    }else if(strcmp(pstrName, "footxml") == 0){
        pFootManagerUI = [[UIManager alloc] init];
        UIDlgBuild* build = [[UIDlgBuild alloc] init];
        NSString *file = [[NSBundle mainBundle] pathForResource:[NSString stringWithUTF8String:value] ofType:@"xml"];
        self.tableFooterView = [build Create:[file UTF8String] :^UIView *(const char *pstrClass) {
            return nil;
        } :pFootManagerUI :self];
    }else if(strcmp(pstrName, "footheight") == 0){
        fFootHeight = atof(value);
    }else if(strcmp(pstrName, "headheight") == 0){
        fHeadHeight = atof(value);
    }else{
        [commonFun setAttribute:pstrName value:value];
    }
}

- (void)setFrame:(CGRect)frame
{
    [super setFrame:frame];
    if (self.tableHeaderView) {
        NSLog(@"%f %f %f %f", self.tableHeaderView.frame.origin.x, self.tableHeaderView.frame.origin.y, self.tableHeaderView.frame.size.width, self.tableHeaderView.frame.size.height);
        [self.tableHeaderView setFrame:CGRectMake(0, 0, frame.size.width, fHeadHeight)];
    }
    
    self.contentInset = UIEdgeInsetsMake(fHeadHeight, 0, fFootHeight, 0);
}
@end


/********************************************************/
/*                                                      */
/********************************************************/
@interface CListElementUI()
{
    CCommonFunUI    *commonFun;
    NSInteger       iLayoutStyle;
}
@end
@implementation CListElementUI
@synthesize iFlag, _iAccessoryType, _iSelectType;
- (id)initWithUIManager:(UIManager*)pManager{
    self = [super init];
    if (self) {
        commonFun = [[CCommonFunUI alloc] initWithDelegateView:self :pManager];
        iLayoutStyle = 0;
        _iAccessoryType = _iSelectType = -1;
#if LISTELEMENT_REMOVESUBVIEW
        for (UIView* view in self.subviews) {
            [view removeFromSuperview];
        }
#endif
    }
    return self;
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier :(UIManager*)pManager
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        commonFun = [[CCommonFunUI alloc] initWithDelegateView:self :pManager];
        iLayoutStyle = 0;
        _iAccessoryType = _iSelectType = -1;
#if LISTELEMENT_REMOVESUBVIEW
        for (UIView* view in self.subviews) {
            [view removeFromSuperview];
        }
#endif
    }
    return self;
}

- (void)setFlag:(NSInteger)flag
{
    iFlag = flag;
}

- (void)setIAccessoryType:(NSInteger)type
{
    _iAccessoryType = type;
}

- (void)setISelectType:(NSInteger)type
{
    _iSelectType = type;
}

- (CCommonFunUI*)commonFun
{
    return commonFun;
}

- (void)SetAttribute:(const char*)pstrName :(const char*)value
{
    if (strcmp(pstrName, "style") == 0) {
        iLayoutStyle = atoi(value);
    }else if(strcmp(pstrName, "accessorytype") == 0){
        _iAccessoryType = atoi(value);
    }else if(strcmp(pstrName, "selecttype") == 0){
        _iSelectType = atoi(value);
    }else{
        [commonFun setAttribute:pstrName value:value];
    }
}

- (void)setFrame:(CGRect)frame
{
    [super setFrame:frame];
    if (iLayoutStyle == 0) {
        [CCommonFunUI SetFrameOfVertical:frame :self];
    }else if(iLayoutStyle == 1){
        [CCommonFunUI SetFrameOfHorizontal:frame :self];
    }
}
@end

/********************************************************/
/*                                                      */
/********************************************************/
@interface CTextUI()
{
    CCommonFunUI    *commonFun;
    CGFloat         fTextIndent;
}
@end
@implementation CTextUI
- (id)initWithUIManager:(UIManager*)pManager{
    self = [super init];
    if (self) {
        commonFun = [[CCommonFunUI alloc] initWithDelegateView:self :pManager];
        self.lineBreakMode = NSLineBreakByWordWrapping;
        self.numberOfLines = 0;
        fTextIndent = 0;
    }
    return self;
}

- (CCommonFunUI*)commonFun
{
    return commonFun;
}

- (void)SetAttribute:(const char*)pstrName :(const char*)value
{
    if (strcmp(pstrName, "text") == 0) {
        [self setText:[commonFun getLanString:value]];
    }else if (strcmp(pstrName, "align") == 0){
        if (strcmp(value, "center") == 0) {
            self.textAlignment = NSTextAlignmentCenter;
        }else if (strcmp(value, "right") == 0){
            self.textAlignment = NSTextAlignmentRight;
        }else{
            self.textAlignment = NSTextAlignmentLeft;
        }
    }else if (strcmp(pstrName, "textcolor") == 0){
        self.textColor = [CCommonFunUI getColorValue:value];
    }else if (strcmp(pstrName, "font") == 0){
        self.font = [UIFont systemFontOfSize:atof(value)];
    }else if(strcmp(pstrName, "textIndent") == 0){
        fTextIndent = atof(value);
    }else{
        [commonFun setAttribute:pstrName value:value];
    }
}

- (void)drawTextInRect:(CGRect)rect
{
    CGRect inset = CGRectMake(rect.origin.x + fTextIndent, rect.origin.y, rect.size.width - fTextIndent, rect.size.height);
    [super drawTextInRect:inset];
}
@end



/********************************************************/
/*                                                      */
/********************************************************/
@interface CComboUI()
{
    CCommonFunUI    *commonFun;
}
@end
@implementation CComboUI
- (id)initWithUIManager:(UIManager*)pManager{
    self = [super init];
    if (self) {
        commonFun = [[CCommonFunUI alloc] initWithDelegateView:self :pManager];
        
    }
    return self;
}

- (CCommonFunUI*)commonFun
{
    return commonFun;
}

- (void)SetAttribute:(const char*)pstrName :(const char*)value
{
    [commonFun setAttribute:pstrName value:value];
}
@end


/********************************************************/
/*                                                      */
/********************************************************/
@interface CLabelUI()
{
    CCommonFunUI    *commonFun;
    CGFloat         fTextIndent;
}
@end
@implementation CLabelUI
- (id)initWithUIManager:(UIManager*)pManager{
    self = [super init];
    if (self) {
        commonFun = [[CCommonFunUI alloc] initWithDelegateView:self :pManager];
        fTextIndent = 0;
    }
    return self;
}

- (CCommonFunUI*)commonFun
{
    return commonFun;
}

- (void)SetAttribute:(const char*)pstrName :(const char*)value
{
    if (strcmp(pstrName, "text") == 0) {
        [self setText:[commonFun getLanString:value]];
    }else if (strcmp(pstrName, "align") == 0){
        if (strcmp(value, "center") == 0) {
            self.textAlignment = NSTextAlignmentCenter;
        }else if (strcmp(value, "right") == 0){
            self.textAlignment = NSTextAlignmentRight;
        }else{
            self.textAlignment = NSTextAlignmentLeft;
        }
    }else if (strcmp(pstrName, "textcolor") == 0){
        self.textColor = [CCommonFunUI getColorValue:value];
    }else if (strcmp(pstrName, "font") == 0){
        self.font = [UIFont systemFontOfSize:atof(value)];
    }else if(strcmp(pstrName, "textIndent") == 0){
        fTextIndent = atof(value);
    }else{
        [commonFun setAttribute:pstrName value:value];
    }
}

- (void)drawTextInRect:(CGRect)rect
{
    CGRect inset = CGRectMake(rect.origin.x + fTextIndent, rect.origin.y, rect.size.width - fTextIndent, rect.size.height);
    [super drawTextInRect:inset];
}
@end


/********************************************************/
/*                                                      */
/********************************************************/
@interface CButtonUI()
{
    UIColor         *normalColor;
    UIColor         *hotColor;
    CCommonFunUI    *commonFun;
    UILabel         *textLabel;
    BOOL            _bDownEvent;
    int             _iLabelAlign;
}
@end
@implementation CButtonUI
- (id)initWithUIManager:(UIManager*)pManager
{
    self = [super init];
    if (self) {
        _bDownEvent = NO;
        hotColor = nil;
        normalColor = nil;
        textLabel = nil;
        _iLabelAlign = 0;
        
        commonFun = [[CCommonFunUI alloc] initWithDelegateView:self :pManager];
        [self addTarget:self action:@selector(onPress:) forControlEvents:UIControlEventTouchUpInside];
    }
    return self;
}

- (void)dealloc
{
}

- (CCommonFunUI*)commonFun
{
    return commonFun;
}

- (void)onPress:(id)sender
{
    [commonFun.pManagerUI OnNotify:self event:FYEVENT_TOUCHUPINSIDE param:NULL];
    if (normalColor) {
        [self setBackgroundColor:normalColor];
    }
}

- (void)onDown:(id)sender
{
    [commonFun.pManagerUI OnNotify:self event:FYEVENT_TOUCHDOWN param:NULL];
    normalColor = [self backgroundColor];
    if (hotColor)[self setBackgroundColor:hotColor];
}

- (void)SetAttribute:(const char*)pstrName :(const char*)value
{
    if (strcmp(pstrName, "normalImage") == 0) {
        [self setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%s", value]] forState:UIControlStateNormal];
    }else if(strcmp(pstrName, "disableImage") == 0){
        [self setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%s", value]] forState:UIControlStateDisabled];
    }else if(strcmp(pstrName, "selectImage") == 0){
        [self setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%s", value]] forState:UIControlStateHighlighted];
    }else if(strcmp(pstrName, "mode") == 0){
        self.imageView.contentMode = atoi(value);
    }else if(strcmp(pstrName, "text") == 0){
        if (textLabel == nil) {
            textLabel = [[UILabel alloc] init];
            [self addSubview:textLabel];
        }
        [textLabel setText:[commonFun getLanString:value]];
    }else if(strcmp(pstrName, "textcolor") == 0){
        if (textLabel == nil) {
            textLabel = [[UILabel alloc] init];
            [self addSubview:textLabel];
        }
        [textLabel setTextColor:[CCommonFunUI getColorValue:value]];
    }else if(strcmp(pstrName, "align") == 0){
        if (textLabel == nil) {
            textLabel = [[UILabel alloc] init];
            [self addSubview:textLabel];
        }
        _iLabelAlign = 0;
        if (strcmp(value, "center") == 0) {
            textLabel.textAlignment = NSTextAlignmentCenter;
        }else if (strcmp(value, "right") == 0){
            textLabel.textAlignment = NSTextAlignmentRight;
        }else if (strcmp(value, "left") == 0){
            textLabel.textAlignment = NSTextAlignmentLeft;
        }else{
            char style[20] = {0};
            char align[20] = {0};
            int s = 0;
            int font = 0;
            _iLabelAlign = 1;
            if (4 == sscanf(value, "%20s %d %20s %d", style, &s, align, &font)) {
                int w = 0, h = 0;
                if (strcmp(style, "top") == 0) {
                    _iLabelAlign = 1;
                    h = s;
                }else if (strcmp(style, "bottom") == 0){
                    _iLabelAlign = 2;
                    h = s;
                }else if (strcmp(style, "left") == 0){
                    _iLabelAlign = 3;
                    w = s;
                }else if (strcmp(style, "right") == 0){
                    _iLabelAlign = 4;
                    w = s;
                }
                if (strcmp(align, "center") == 0) {
                    textLabel.textAlignment = NSTextAlignmentCenter;
                }else if (strcmp(align, "right") == 0){
                    textLabel.textAlignment = NSTextAlignmentRight;
                }else {
                    textLabel.textAlignment = NSTextAlignmentLeft;
                }
                [textLabel setFont:[UIFont systemFontOfSize:font]];
                [textLabel setFrame:CGRectMake(0, 0, w, h)];
            }
        
        }
    }else if(strcmp(pstrName, "hotcolor") == 0){
        if (!_bDownEvent) {
            _bDownEvent = YES;
            [self addTarget:self action:@selector(onDown:) forControlEvents:UIControlEventTouchDown];
        }
        hotColor = [CCommonFunUI getColorValue:value];
    }else if(strcmp(pstrName, "downEvent") == 0){
        if (!_bDownEvent) {
            _bDownEvent = YES;
            [self addTarget:self action:@selector(onDown:) forControlEvents:UIControlEventTouchDown];
        }
    }else{
        [commonFun setAttribute:pstrName value:value];
    }
}

- (void)setFrame:(CGRect)frame
{
    [super setFrame:frame];
    if (textLabel) {
        switch (_iLabelAlign) {
            case 0:
                [textLabel setFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
                break;
            case 1:
            {
                [textLabel setFrame:CGRectMake(0, -textLabel.frame.size.height-5, frame.size.width, textLabel.frame.size.height)];
            }
                break;
            case 2:
            {
                [textLabel setFrame:CGRectMake(0, frame.size.height + 5, frame.size.width, textLabel.frame.size.height)];
            }
                break;
            case 3:
            {
                [textLabel setFrame:CGRectMake(-textLabel.frame.size.width-5, 0, textLabel.frame.size.width, frame.size.height)];
            }
                break;
            case 4:
            {
                [textLabel setFrame:CGRectMake(frame.size.width+5, 0, textLabel.frame.size.width, frame.size.height)];
            }
                break;
            default:
                break;
        }
    }
}

@end


/********************************************************/
/*                                                      */
/********************************************************/
@interface CSwitchUI()
{
    CCommonFunUI    *commonFun;
}
@end
@implementation CSwitchUI
- (id)initWithUIManager:(UIManager*)pManager{
    self = [super init];
    if (self) {
        commonFun = [[CCommonFunUI alloc] initWithDelegateView:self :pManager];
        self.delegate = self;
    }
    return self;
}

- (CCommonFunUI*)commonFun{
    return commonFun;
}

- (void)TheButtonIsPress:(BOOL)on WithSwithTag:(int)tag{
    [commonFun.pManagerUI OnNotify:self event:FYEVENT_SW_SELECTED param:NULL];
}

- (void)SetAttribute:(const char*)pstrName :(const char*)value{
    if (strcmp(pstrName, "oncolor") == 0) {
        self.onTintColor = [CCommonFunUI getColorValue:value];
    }else if (strcmp(pstrName, "offcolor") == 0) {
        self.offTintColor = [CCommonFunUI getColorValue:value];
    }else if (strcmp(pstrName, "ontext") == 0) {
        self.onText = [commonFun getLanString:value];
    }else if (strcmp(pstrName, "offtext") == 0) {
        self.offText = [commonFun getLanString2:value];
    }else if (strcmp(pstrName, "text") == 0) {
        if ([commonFun getTextTag:1]) {
            self.onText = [commonFun getLanString:[[commonFun getTextTag:1] UTF8String]];
        }
        if ([commonFun getTextTag:2]) {
            self.offText = [commonFun getLanString2:[[commonFun getTextTag:2] UTF8String]];
        }
    }else{
        [commonFun setAttribute:pstrName value:value];
    }
    
}
@end

/********************************************************/
/*                                                      */
/********************************************************/
@interface COptionUI()
{
    CCommonFunUI    *commonFun;
    NSString        *sGroup;
    UIImage         *imgNormal;
    UIImage         *imgSelected;
    UIImageView     *imageView;
    UILabel         *labelView;
    UIColor         *labelNorColor;
    UIColor         *labelSelColor;
}
@end
@implementation COptionUI
@synthesize selected, tag;
- (id)initWithUIManager:(UIManager*)pManager
{
    self = [super init];
    if (self) {
        commonFun = [[CCommonFunUI alloc] initWithDelegateView:self :pManager];
        imageView = [[UIImageView alloc] init];
        labelView = nil;
        UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(click:)];
        [self addGestureRecognizer:tap];
        //[imageView addGestureRecognizer:tap];
        [self addSubview:imageView];
        sGroup = nil;
        imgNormal = nil;
        imgSelected = nil;
        imageView.contentMode = UIViewContentModeScaleToFill;
    }
    return self;
}

- (void)setFrame:(CGRect)frame
{
    [super setFrame:frame];
    if (labelView) {
        [imageView setFrame:CGRectMake(0, 0, frame.size.width, frame.size.height-labelView.frame.size.height-5)];
        [labelView setFrame:CGRectMake(0, frame.size.height-labelView.frame.size.height, frame.size.width, labelView.frame.size.height)];
    }else{
        [imageView setFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
    }
}

- (void)setTag:(NSInteger)t
{
    tag = t;
}

- (void)click:(id)sender
{
    if (sGroup) {
        [commonFun.pManagerUI ChangeOptionSelectObject:self :sGroup];
    }else{
        [self setSelected:![self isSelected]];
    }
    [commonFun.pManagerUI OnNotify:self event:FYEVENT_OP_SELECTED param:NULL];
}

- (void)setSelected:(BOOL)bSelected
{
    selected = bSelected;
    if (selected) {
        if(imgSelected) imageView.image = imgSelected;
        if (labelView) labelView.textColor = labelSelColor;
    }else{
        if(imgNormal) imageView.image = imgNormal;
        if (labelView) labelView.textColor = labelNorColor;
    }
}

- (BOOL)isSelected
{
    return selected;
}

- (CCommonFunUI*)commonFun
{
    return commonFun;
}

- (void)SetAttribute:(const char*)pstrName :(const char*)value
{
    if (strcmp(pstrName, "group") == 0) {
        if (strcmp(value, "")) {
            sGroup = [NSString stringWithUTF8String:value];
            if (selected) {
                [commonFun.pManagerUI ChangeOptionSelectObject:self :sGroup];
                if (imgSelected) {
                    imageView.image = imgSelected;
                    [commonFun.pManagerUI OnNotify:self event:FYEVENT_OP_SELECTED param:NULL];
                }
            }
        }
    }else if (strcmp(pstrName, "normalImage") == 0){
        imgNormal = [UIImage imageNamed:[NSString stringWithUTF8String:value]];
        if (!selected) {
            imageView.image = imgNormal;
            
        }
    }else if (strcmp(pstrName, "selectedImage") == 0){
        imgSelected = [UIImage imageNamed:[NSString stringWithUTF8String:value]];
        if (selected) {
            imageView.image = imgSelected;
            [commonFun.pManagerUI OnNotify:self event:FYEVENT_OP_SELECTED param:NULL];
        }
    }else if(strcmp(pstrName, "selected") == 0){
        selected = atoi(value);
        if (sGroup) {
            [commonFun.pManagerUI ChangeOptionSelectObject:self :sGroup];
        }
        if (imgSelected && selected) {
            imageView.image = imgSelected;
            [commonFun.pManagerUI OnNotify:self event:FYEVENT_OP_SELECTED param:NULL];
        }
        if (labelView && selected && labelSelColor) {
            labelView.textColor = labelSelColor;
        }
    }else if(strcmp(pstrName, "usrtag") == 0){
        tag = atoi(value);
    }else if(strcmp(pstrName, "mode") == 0){
        imageView.contentMode = atoi(value);
    }else if(strcmp(pstrName, "label") == 0){
        int fontsize = 16;
        int height = 10;
        char text[256] = {0};
        char style[20] = {0};
        char norColor[20] = {0};
        char selColor[20] = {0};
        if (6 == sscanf(value, "%d %256s %d %20s %20s %20s", &fontsize, text, &height, style, norColor, selColor)) {
            if (labelView == nil) {
                labelView = [[UILabel alloc] init];
                [self addSubview:labelView];
            }
            labelView.font = [UIFont systemFontOfSize:fontsize];
            if (strcmp(style, "center") == 0) {
                labelView.textAlignment = NSTextAlignmentCenter;
            }else if (strcmp(style, "right") == 0){
                labelView.textAlignment = NSTextAlignmentRight;
            }else{
                labelView.textAlignment = NSTextAlignmentLeft;
            }
            labelView.text = [commonFun getLanString:text];
            [labelView setFrame:CGRectMake(0, 0, 0, height)];
            labelNorColor = [CCommonFunUI getColorValue:norColor];
            labelSelColor = [CCommonFunUI getColorValue:selColor];
            if (selected) {
                labelView.textColor = labelSelColor;
            }else{
                labelView.textColor = labelNorColor;
            }
        }
    }else if(strcmp(pstrName, "text") == 0){
        if (labelView) {
            labelView.text = [commonFun getLanString:value];
        }
    }else{
        [commonFun setAttribute:pstrName value:value];
    }
}
@end
/********************************************************/
/*                                                      */
/********************************************************/
@interface CSliderUI()
{
    CCommonFunUI    *commonFun;
}
@end
@implementation CSliderUI
- (id)initWithUIManager:(UIManager*)pManager{
    self = [super init];
    if (self) {
        commonFun = [[CCommonFunUI alloc] initWithDelegateView:self :pManager];
        
    }
    return self;
}

- (CCommonFunUI*)commonFun
{
    return commonFun;
}

- (void)SetAttribute:(const char*)pstrName :(const char*)value
{
    [commonFun setAttribute:pstrName value:value];
}
@end

/********************************************************/
/*                                                      */
/********************************************************/
@interface CMBSliderUI()
{
    CCommonFunUI    *commonFun;
}
@end
@implementation CMBSliderUI
- (id)initWithUIManager:(UIManager*)pManager{
    self = [super init];
    if (self) {
        commonFun = [[CCommonFunUI alloc] initWithDelegateView:self :pManager];
        [self setDelegate:self];
    }
    return self;
}

- (void) sliderDidSlide:(MBSliderView *)slideView {
    // Customization example
    // Just do whatever you want
    [commonFun.pManagerUI OnNotify:self event:FYEVENT_MBSLIDER_SLIDER param:NULL];
}

- (CCommonFunUI*)commonFun
{
    return commonFun;
}

- (void)SetAttribute:(const char*)pstrName :(const char*)value
{
    if (strcmp(pstrName, "thumbcolor") == 0) {
        [self setThumbColor:[CCommonFunUI getColorValue:value]];
    }else if (strcmp(pstrName, "labelcolor") == 0) {
        [self setLabelColor:[CCommonFunUI getColorValue:value]];
    }else if (strcmp(pstrName, "thumbimage") == 0) {
        [self setThumbImage:[UIImage imageNamed:[NSString stringWithUTF8String:value]]];
    }else if (strcmp(pstrName, "text") == 0) {
        [self setText:[commonFun getLanString:value]];
    }
    [commonFun setAttribute:pstrName value:value];
}
@end

/********************************************************/
/*                                                      */
/********************************************************/
@interface CNumberUI()
{
    CCommonFunUI    *commonFun;
}
@end
@implementation CNumberUI
- (id)initWithUIManager:(UIManager*)pManager{
    self = [super init];
    if (self) {
        commonFun = [[CCommonFunUI alloc] initWithDelegateView:self :pManager];
        
    }
    return self;
}

- (CCommonFunUI*)commonFun
{
    return commonFun;
}

- (void)SetAttribute:(const char*)pstrName :(const char*)value
{
    [commonFun setAttribute:pstrName value:value];
}
@end


/********************************************************/
/*                                                      */
/********************************************************/
@interface CControlUI()
{
    CCommonFunUI    *commonFun;
}
@end
@implementation CControlUI
- (id)initWithUIManager:(UIManager*)pManager{
    self = [super init];
    if (self) {
        commonFun = [[CCommonFunUI alloc] initWithDelegateView:self :pManager];
        
    }
    return self;
}

- (CCommonFunUI*)commonFun
{
    return commonFun;
}

- (void)SetAttribute:(const char*)pstrName :(const char*)value
{
    [commonFun setAttribute:pstrName value:value];
}
@end


/********************************************************/
/*                                                      */
/********************************************************/
@interface CTimeUI()
{
    CCommonFunUI    *commonFun;
}
@end
@implementation CTimeUI
- (id)initWithUIManager:(UIManager*)pManager{
    self = [super init];
    if (self) {
        commonFun = [[CCommonFunUI alloc] initWithDelegateView:self :pManager];
        
    }
    return self;
}

- (CCommonFunUI*)commonFun
{
    return commonFun;
}

- (void)SetAttribute:(const char*)pstrName :(const char*)value
{
    [commonFun setAttribute:pstrName value:value];
}
@end


/********************************************************/
/*                                                      */
/********************************************************/
@interface CProgressUI()
{
    CCommonFunUI    *commonFun;
}
@end
@implementation CProgressUI
- (id)initWithUIManager:(UIManager*)pManager{
    self = [super init];
    if (self) {
        commonFun = [[CCommonFunUI alloc] initWithDelegateView:self :pManager];
        
    }
    return self;
}

- (CCommonFunUI*)commonFun
{
    return commonFun;
}

- (void)SetAttribute:(const char*)pstrName :(const char*)value
{
    [commonFun setAttribute:pstrName value:value];
}
@end


/********************************************************/
/*                                                      */
/********************************************************/
@interface CRichEditUI()
{
    CCommonFunUI    *commonFun;
}
@end
@implementation CRichEditUI
- (id)initWithUIManager:(UIManager*)pManager{
    self = [super init];
    if (self) {
        commonFun = [[CCommonFunUI alloc] initWithDelegateView:self :pManager];
        
    }
    return self;
}

- (CCommonFunUI*)commonFun
{
    return commonFun;
}

- (void)SetAttribute:(const char*)pstrName :(const char*)value
{
    [commonFun setAttribute:pstrName value:value];
}
@end


/********************************************************/
/*                                                      */
/********************************************************/
@interface CDateUI()
{
    CCommonFunUI    *commonFun;
}
@end
@implementation CDateUI
- (id)initWithUIManager:(UIManager*)pManager{
    self = [super init];
    if (self) {
        commonFun = [[CCommonFunUI alloc] initWithDelegateView:self :pManager];
        
    }
    return self;
}

- (CCommonFunUI*)commonFun
{
    return commonFun;
}

- (void)SetAttribute:(const char*)pstrName :(const char*)value
{
    [commonFun setAttribute:pstrName value:value];
}
@end


/********************************************************/
/*                                                      */
/********************************************************/
@interface CContainerUI()
{
    CCommonFunUI    *commonFun;
}
@end
@implementation CContainerUI
- (id)initWithUIManager:(UIManager*)pManager{
    self = [super init];
    if (self) {
        commonFun = [[CCommonFunUI alloc] initWithDelegateView:self :pManager];
        
    }
    return self;
}

- (CCommonFunUI*)commonFun
{
    return commonFun;
}

- (void)SetAttribute:(const char*)pstrName :(const char*)value
{
    [commonFun setAttribute:pstrName value:value];
}
@end


/********************************************************/
/*                                                      */
/********************************************************/
@interface CTabLayoutUI()
{
    CCommonFunUI    *commonFun;
    NSInteger       m_iSelectIndex;
    NSMutableArray  *m_tabArray;
}
@end
@implementation CTabLayoutUI
- (id)initWithUIManager:(UIManager*)pManager{
    self = [super init];
    if (self) {
        commonFun = [[CCommonFunUI alloc] initWithDelegateView:self :pManager];
        m_tabArray = [[NSMutableArray alloc] init];
    }
    return self;
}

- (CCommonFunUI*)commonFun
{
    return commonFun;
}

- (void)AddTabElement:(UIView *)element{
    [m_tabArray addObject:element];
    if ([m_tabArray count] > 1) {
        [element setHidden:YES];
    }
}

- (void)SetAttribute:(const char*)pstrName :(const char*)value
{
    [commonFun setAttribute:pstrName value:value];
}

- (void)SelectIndex:(int)index{
    if (index < [m_tabArray count]) {
        [[m_tabArray objectAtIndex:m_iSelectIndex] setHidden:YES];
        [[m_tabArray objectAtIndex:index] setFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
        [[m_tabArray objectAtIndex:index] setHidden:NO];
        m_iSelectIndex = index;
    }
}

- (void)setNeedsDisplay{
    [super setNeedsDisplay];
    [self setFrame:self.frame];
}

- (void)setFrame:(CGRect)frame{
    [super setFrame:frame];
    int count = (int)[m_tabArray count];
    for (int i=0; i < count; i++) {
        if (![[m_tabArray objectAtIndex:i] isHidden]) {
            [[m_tabArray objectAtIndex:i] setFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
            break;
        }
    }
}
@end


/********************************************************/
/*                                                      */
/********************************************************/
@interface CScrollViewUI()
{
    CCommonFunUI    *commonFun;
    CGSize          szContentSize;
    BOOL            bHasContextEx;
    CGSize          szContentSizeEx;
    NSInteger       iLayoutStyle;
    BOOL            bHasLeft;
    BOOL            bHasRight;
    float           fLeftValue;
    float           fRightValue;
}
@end
@implementation CScrollViewUI
- (id)initWithUIManager:(UIManager*)pManager{
    self = [super init];
    if (self) {
        commonFun = [[CCommonFunUI alloc] initWithDelegateView:self :pManager];
        self.delegate = self;
        iLayoutStyle = 0;
        bHasContextEx = NO;
        bHasLeft = bHasRight = NO;
        fLeftValue = fRightValue = 0;
    }
    return self;
}

- (CCommonFunUI*)commonFun
{
    return commonFun;
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    [commonFun.pManagerUI OnNotify:self event:FYEVENT_SCROLLDRAGBEGIN param:NULL];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    [commonFun.pManagerUI OnNotify:self event:FYEVENT_SCROLLENDDECELERAT param:NULL];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    if (![commonFun.pManagerUI OnNotify:self event:FYEVENT_SCROLLDRAGEND param:NULL]) {
        if (self.contentOffset.x > 0) {
            if (bHasRight) {
                if (fRightValue / 2 < self.contentOffset.x) {
                    [self setContentOffset:CGPointMake(fRightValue, self.contentOffset.y) animated:YES];
                }else{
                    [self setContentOffset:CGPointMake(0, self.contentOffset.y) animated:YES];
                }
            }
        }else{
            if (bHasLeft) {
                if (-fLeftValue / 2 > self.contentOffset.x) {
                    [self setContentOffset:CGPointMake(-fLeftValue, self.contentOffset.y) animated:YES];
                }else{
                    [self setContentOffset:CGPointMake(0, self.contentOffset.y) animated:YES];
                }
            }
        }
    }
}

- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{
    if (![commonFun.pManagerUI OnNotify:self event:FYEVENT_SCROLLWILLBEGINDECELERAT param:NULL]) {
        if (self.contentOffset.x > 0) {
            if (bHasRight) {
                if (fRightValue / 2 < self.contentOffset.x) {
                    [self setContentOffset:CGPointMake(fRightValue, self.contentOffset.y) animated:YES];
                }else{
                    [self setContentOffset:CGPointMake(0, self.contentOffset.y) animated:YES];
                }
            }
        }else{
            if (bHasLeft) {
                if (-fLeftValue / 2 > self.contentOffset.x) {
                    [self setContentOffset:CGPointMake(-fLeftValue, self.contentOffset.y) animated:YES];
                }else{
                    [self setContentOffset:CGPointMake(0, self.contentOffset.y) animated:YES];
                }
            }
        }
    }
}

- (void)SetAttribute:(const char*)pstrName :(const char*)value
{
    if (strcmp(pstrName, "contentsize") == 0) {
        szContentSize = [CCommonFunUI getSizeValue:value];
    }else if(strcmp(pstrName, "contentsizeex") == 0){
        bHasContextEx = YES;
        szContentSizeEx = [CCommonFunUI getSizeValue:value];
    }else if(strcmp(pstrName, "style") ==0 ){
        iLayoutStyle = atoi(value);
    }else if(strcmp(pstrName, "scrollbarhide") == 0){
        if (strcmp(value, "true") == 0) {
            self.showsVerticalScrollIndicator = FALSE;
            self.showsHorizontalScrollIndicator = FALSE;
        }else{
            self.showsVerticalScrollIndicator = YES;
            self.showsHorizontalScrollIndicator = YES;
        }
    }else if (strcmp(pstrName, "hasleft") == 0){
        if (strcmp(value, "true") == 0) {
            bHasLeft = YES;
        }else{
            bHasLeft = NO;
        }
    }else if (strcmp(pstrName, "hasright") == 0){
        if (strcmp(value, "true") == 0) {
            bHasRight = YES;
        }else{
            bHasRight = NO;
        }
    }else if(strcmp(pstrName, "leftvalue") == 0){
        fLeftValue = atof(value);
    }else if(strcmp(pstrName, "rightvalue") == 0){
        fRightValue = atof(value);
    }else{
        [commonFun setAttribute:pstrName value:value];
    }
}

- (void)setFrame:(CGRect)frame{
    [super setFrame:frame];
    if (bHasContextEx) {
        frame.size.width += szContentSizeEx.width;
        frame.size.height += szContentSizeEx.height;
        [self setContentSize:frame.size];
        
        if (iLayoutStyle == 0) {
            [CCommonFunUI SetFrameOfVertical:frame :self];
        }else if(iLayoutStyle == 1){
            [CCommonFunUI SetFrameOfHorizontal:frame :self];
        }else if (iLayoutStyle == 2){
            [CCommonFunUI SetFrameOfOverlap:frame :self];
        }
    }else{
        CGSize size = szContentSize;
        if (szContentSize.width == 0) size.width = frame.size.width;
        if (szContentSize.height == 0) size.height = frame.size.height;
        [self setContentSize:size];
        
        if (iLayoutStyle == 0) {
            [CCommonFunUI SetFrameOfVertical:frame :self];
        }else if(iLayoutStyle == 1){
            [CCommonFunUI SetFrameOfHorizontal:frame :self];
        }else if (iLayoutStyle == 2){
            [CCommonFunUI SetFrameOfOverlap:frame :self];
        }
    }
}
@end

/********************************************************/
/*                                                      */
/********************************************************/
@interface CLayoutUI()
{
    CCommonFunUI    *commonFun;
    NSInteger       iLayoutStyle;
}
@end
@implementation CLayoutUI
@synthesize tag;
- (id)initWithUIManager:(UIManager*)pManager{
    self = [super init];
    if (self) {
        commonFun = [[CCommonFunUI alloc] initWithDelegateView:self :pManager];
        iLayoutStyle = 0;
        tag = 0;
    }
    return self;
}

- (void)setTag:(NSInteger)t {
    tag = t;
}

- (void)SetAttribute:(const char*)pstrName :(const char*)value
{
    if (strcmp(pstrName, "style") == 0) {
        iLayoutStyle = atoi(value);
    }else if(strcmp(pstrName, "clip") == 0){
        self.clipsToBounds = atoi(value)%2;
    }else{
        [commonFun setAttribute:pstrName value:value];
    }
}

- (void)setNeedsDisplay
{
    [super setNeedsDisplay];
    [self setFrame:self.frame];
}

- (void)setFrame:(CGRect)frame
{
    [super setFrame:frame];
    if (iLayoutStyle == 0) {
        [CCommonFunUI SetFrameOfVertical:frame :self];
    }else if(iLayoutStyle == 1){
        [CCommonFunUI SetFrameOfHorizontal:frame :self];
    }else if (iLayoutStyle == 3){
        [CCommonFunUI SetFrameOfOverlap:frame :self];
    }
}

- (CCommonFunUI*)commonFun
{
    return commonFun;
}
@end
